// DlgManage.cpp : implementation file
//

#include "stdafx.h"
#include "finance.h"
#include "financedef.h"
#include "CDataFile.h"

#include "DlgManage.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


extern CFinanceApp theApp;       


// 
// Table name for management in Mmoney.mdb
CHAR	*g_chTable[] = {	
						"Purpose",
						"Userinfo"
					};


/////////////////////////////////////////////////////////////////////////////
// CDlgManage dialog

// 
//------------------------------------------------------------
CDlgManage::CDlgManage(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgManage::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgManage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

// 
//------------------------------------------------------------
CDlgManage::~CDlgManage()
{
	if( m_pRecordset != NULL )
	{
		if( m_pRecordset->State )
			m_pRecordset->Close();
		m_pRecordset = NULL;
	}
}


void CDlgManage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgManage)
	DDX_Control(pDX, IDC_LST_CONTENT, m_lstContent);
	DDX_Control(pDX, IDC_BTN_DELE, m_btnDele);
	DDX_Control(pDX, ID_ADD, m_btnAppend);
	DDX_Control(pDX, IDCANCEL, m_btnQuit);
	DDX_Control(pDX, IDC_CMB_FLOW, m_cmbFlow);
	DDX_Control(pDX, IDC_CMB_TABLE, m_cmbTable);
	DDX_Control(pDX, IDC_ED_APPEND, m_edInput);
	DDX_Control(pDX, IDC_ED_PWD, m_edPwd);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgManage, CDialog)
	//{{AFX_MSG_MAP(CDlgManage)
	ON_BN_CLICKED(ID_ADD, OnAdd)
	ON_BN_CLICKED(IDC_BTN_DELE, OnBtnDele)
	ON_CBN_SELCHANGE(IDC_CMB_TABLE, OnSelchangeCmbTable)
	ON_CBN_SELCHANGE(IDC_CMB_FLOW, OnSelchangeCmbFlow)
	ON_LBN_SELCHANGE(IDC_LST_CONTENT, OnSelchangeLstContent)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgManage message handlers

// 
// 
//------------------------------------------------------------
BOOL CDlgManage::OnInitDialog() 
{

	CDialog::OnInitDialog();
	
	// <> Init UI	
	m_btnDele.SetIcon(IDI_RIGHT1, IDI_RIGHT);
	m_btnAppend.SetIcon(IDI_LEFT1, IDI_LEFT);
	m_btnQuit.SetIcon(IDI_RUN2, IDI_RUN3);
	
	m_edInput.SetLimitText( 48 );
	m_edPwd.SetLimitText( 48 );
	
	// <> Open RecordSet
	m_pRecordset.CreateInstance(__uuidof(Recordset));


	// <> Init IDC_CMB_TABLE
	m_cmbTable.ResetContent();
	m_cmbTable.AddString( g_chTable[0] );
	m_cmbTable.AddString( g_chTable[1] );
	m_cmbTable.SetCurSel( 0 );
	

	// <> Init IDC_CMB_FLOW and IDC_LST_CONTENT
	OnSelchangeCmbTable();


	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

// 
//------------------------------------------------------------
void CDlgManage::OnAdd() 
{
	_variant_t		var;
	CString			csSQL = "SELECT * FROM ";
	CString			csTmp, csInput;
	

	UpdateData( TRUE );

	// check null
	m_edInput.GetWindowText( csInput );
	if( csInput.IsEmpty() ) 
	{	
		MessageBox( "Name must be input!", "Remind", MB_ICONWARNING );
		m_edInput.SetFocus();
		return ;
	}

	// check same name
	if( m_lstContent.FindString( -1, csInput ) != LB_ERR )
	{	
		MessageBox( "This name existed in the table!\nPlease rename!", "Remind", MB_ICONWARNING );
		return ;
	}
	

	m_cmbTable.GetLBText( m_cmbTable.GetCurSel(), csTmp );
	if( csTmp == g_chTable[0] ) // Purpose
		csSQL += g_chTable[0];
	else	// userinfo
		csSQL += g_chTable[1];

	// <> Open table waiting for append
	if( m_pRecordset->State )
		m_pRecordset->Close();

	var = csSQL;
	try
	{
		m_pRecordset->Open( var,   
			theApp.m_pConnection.GetInterfacePtr(),	 // Get the Ptr of connect lib
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	
	try
	{
		m_pRecordset->AddNew();    
		
		if( csTmp == g_chTable[0] ) // Purpose
		{
			var = csInput;
			m_pRecordset->PutCollect( "Usename", var );	// Usename

			m_cmbFlow.GetLBText( m_cmbFlow.GetCurSel(), csTmp );
			var = csTmp;
			m_pRecordset->PutCollect( "Flowclass", var );	// Flowclass
		}
		else
		{
			var = csInput;
			m_pRecordset->PutCollect( "Name", var );	// Name
			
			m_edPwd.GetWindowText( csTmp );
			if( csTmp.IsEmpty() ) 
				csTmp = IDS_DEFAULTPWD;	// default pwd 
			var = csTmp;
			m_pRecordset->PutCollect( "Pwd", var );	// Pwd
		}

		// <> synchronize the listbox
		m_lstContent.AddString( csInput );

		m_pRecordset->Update();
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}

	if( m_pRecordset->State )
		m_pRecordset->Close();
}

// 
//------------------------------------------------------------
void CDlgManage::OnBtnDele() 
{
	_variant_t		var;
	CString			csInfo = "( ";
	CString			csTable, csTmp;
	

	INT	iLstSel = m_lstContent.GetCurSel();
	INT nCount = m_lstContent.GetCount();
	if( (iLstSel == LB_ERR) || (nCount <= 0) )
		return ;
	
	m_lstContent.GetText( iLstSel, csTmp );
	csInfo += csTmp;
	csInfo += " )\n";
	csInfo +="You will delete this record.\n  Are you sure?";
	
	if( MessageBox( csInfo, "Remind", MB_ICONQUESTION|MB_YESNO ) != IDYES ) 
	{
		return ;
	}
	
	csInfo = "SELECT * FROM ";
	m_cmbTable.GetLBText( m_cmbTable.GetCurSel(), csTable );
	if( csTable == g_chTable[0] ) // Purpose
	{
		csInfo += g_chTable[0];
		csInfo += " WHERE Usename='";
	}
	else	// userinfo
	{
		csInfo += g_chTable[1];
		csInfo += " WHERE Name='";
	}
	csInfo += csTmp;
	csInfo += "'";
	
	var = csInfo;
	// open and delete
	try
	{
		m_pRecordset->Open( var, 
			theApp.m_pConnection.GetInterfacePtr(),	 // Get the Ptr of connect lib
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	
		m_pRecordset->Delete( adAffectCurrent );
		m_pRecordset->Update();
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}

	if( m_pRecordset->State )
		m_pRecordset->Close();

	// After Delete(), before Move(), the deleted position will be remain !
	
	// determine the m_iLstCurSel after delete()
	m_lstContent.DeleteString( m_lstContent.GetCurSel() );
}


// 
// switch table
//------------------------------------------------------------
void CDlgManage::OnSelchangeCmbTable() 
{
	_variant_t		var;
	CString			csTmp;
	CString			csSQL;
	

	if( m_pRecordset->State )
		m_pRecordset->Close();

	m_cmbTable.GetLBText( m_cmbTable.GetCurSel(), csTmp );

	if( csTmp == g_chTable[0] ) 
	{
		m_edPwd.EnableWindow( FALSE );
		m_edPwd.ShowWindow( SW_HIDE );
		m_cmbFlow.EnableWindow( TRUE );
		m_cmbFlow.ShowWindow( SW_SHOW );
		
		// <> Fill ComboBox flowclass
		try
		{
			m_pRecordset->Open("SELECT * FROM Flowclass",   
				theApp.m_pConnection.GetInterfacePtr(),	 // Get the Ptr of connect lib
				adOpenDynamic,
				adLockOptimistic,
				adCmdText);
		}
		catch(_com_error *e)
		{
			AfxMessageBox(e->ErrorMessage());
		}
		
		m_cmbFlow.ResetContent();
		try
		{
			if(!m_pRecordset->BOF)
				m_pRecordset->MoveFirst();
			else
			{
				//AfxMessageBox("Table is NULL");
				return ;
			}
			
			while(!m_pRecordset->adoEOF)
			{
				var = m_pRecordset->GetCollect("Classname"); // txt
				if(var.vt != VT_NULL)
					csTmp = (LPCSTR)_bstr_t(var);
				
				m_cmbFlow.AddString( csTmp );
				
				m_pRecordset->MoveNext();
			}
		}
		catch(_com_error *e)
		{
			AfxMessageBox(e->ErrorMessage());
		}
		m_cmbFlow.SetCurSel( 0 );
		m_pRecordset->Close();
		

		// <> Init IDC_LST_CONTENCT
		m_cmbFlow.GetLBText( m_cmbFlow.GetCurSel(), csTmp );	
		csSQL = "SELECT Usename FROM Purpose WHERE Flowclass='" + csTmp + "' ORDER BY Usename ASC";
		var = csSQL;
		try
		{
			m_pRecordset->Open( var,					// const _variant_t & Source
				theApp.m_pConnection.GetInterfacePtr(),	// Get the Ptr of connect lib
				adOpenDynamic,
				adLockOptimistic,
				adCmdText);
		}
		catch(_com_error *e)
		{
			AfxMessageBox(e->ErrorMessage());
		}
		
		m_lstContent.ResetContent();
		try
		{
			if(!m_pRecordset->BOF)
				m_pRecordset->MoveFirst();
			else
			{
				//AfxMessageBox("Table is NULL");
				return ;
			}
			
			while(!m_pRecordset->adoEOF)
			{
				var = m_pRecordset->GetCollect("Usename"); // txt
				if(var.vt != VT_NULL)
					csTmp = (LPCSTR)_bstr_t(var);
				
				m_lstContent.AddString( csTmp );
				
				m_pRecordset->MoveNext();
			}
		}
		catch(_com_error *e)
		{
			AfxMessageBox(e->ErrorMessage());
		}
		m_lstContent.SetCurSel( 0 );
		m_pRecordset->Close();
	}
	else
	{
		m_cmbFlow.EnableWindow( FALSE );
		m_cmbFlow.ShowWindow( SW_HIDE );
		m_edPwd.EnableWindow( TRUE );
		m_edPwd.ShowWindow( SW_SHOW );
		
		try
		{
			m_pRecordset->Open( "SELECT Name FROM Userinfo ORDER BY Name ASC",					// const _variant_t & Source
				theApp.m_pConnection.GetInterfacePtr(),	// Get the Ptr of connect lib
				adOpenDynamic,
				adLockOptimistic,
				adCmdText);
		}
		catch(_com_error *e)
		{
			AfxMessageBox(e->ErrorMessage());
		}
		
		m_lstContent.ResetContent();
		try
		{
			if(!m_pRecordset->BOF)
				m_pRecordset->MoveFirst();
			else
			{
				//AfxMessageBox("Table is NULL");
				return ;
			}
			
			while(!m_pRecordset->adoEOF)
			{
				var = m_pRecordset->GetCollect("Name"); // txt
				if(var.vt != VT_NULL)
				{
					csTmp = (LPCSTR)_bstr_t(var);
					m_lstContent.AddString( csTmp );
				}

				m_pRecordset->MoveNext();
			}
		}
		catch(_com_error *e)
		{
			AfxMessageBox(e->ErrorMessage());
		}
		m_lstContent.SetCurSel( 0 );
		m_pRecordset->Close();

		// <> set pwd into edit
		OnSelchangeLstContent();
	}
	
}

// 
//------------------------------------------------------------
void CDlgManage::OnSelchangeCmbFlow() 
{
	_variant_t		var;
	CString			csTmp;
	CString			csSQL;
	
	
	if( m_pRecordset->State )
		m_pRecordset->Close();

	// <> Fill ComboBox Purpose
	m_cmbFlow.GetLBText( m_cmbFlow.GetCurSel(), csTmp );
	
	csSQL = "SELECT Usename FROM Purpose WHERE Flowclass='" + csTmp + "' ORDER BY Usename ASC";
	var = csSQL;
	try
	{
		m_pRecordset->Open( var,					// const _variant_t & Source
			theApp.m_pConnection.GetInterfacePtr(),	// Get the Ptr of connect lib
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	m_lstContent.ResetContent();
	try
	{
		if(!m_pRecordset->BOF)
			m_pRecordset->MoveFirst();
		else
		{
			//AfxMessageBox("Table is NULL");
			return ;
		}
		
		while(!m_pRecordset->adoEOF)
		{
			var = m_pRecordset->GetCollect("Usename"); // txt
			if(var.vt != VT_NULL)
				csTmp = (LPCSTR)_bstr_t(var);
			
			m_lstContent.AddString( csTmp );
			
			m_pRecordset->MoveNext();
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
	
	m_lstContent.SetCurSel( 0 );
	m_pRecordset->Close();
}

// 
// Only for Userinfo table
//------------------------------------------------------------
void CDlgManage::OnSelchangeLstContent() 
{
	_variant_t		var;
	CString			csTmp;
	CString			csSQL = "SELECT Pwd FROM Userinfo WHERE Name='";
	

	m_cmbTable.GetLBText( m_cmbTable.GetCurSel(), csTmp );
	if( csTmp == g_chTable[0] ) 
		return ;		


	if( m_pRecordset->State )
		m_pRecordset->Close();

	m_lstContent.GetText( m_lstContent.GetCurSel(), csTmp );
	csSQL += csTmp;
	csSQL += "'";
	var = csSQL;
	try
	{
		m_pRecordset->Open( var,					// const _variant_t & Source
			theApp.m_pConnection.GetInterfacePtr(),	// Get the Ptr of connect lib
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);

		var = m_pRecordset->GetCollect( (_variant_t)(long)0 ); // txt
		if(var.vt != VT_NULL)
			csTmp = (LPCSTR)_bstr_t(var);
		else
			csTmp.Empty();
		m_edPwd.SetWindowText( csTmp );
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}

	m_pRecordset->Close();
	
}

